<?php

namespace App\Http\Controllers\Backend\Flaw;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Models\Flaw\Level;
use App\Models\Flaw\Vuls;

class LevelController extends Controller
{
    /**
     * levelList 等级评分列表
     *
     * @param Request $request  string
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function  levelList(Request $request){
        $lists =  $request->type ? Level::getLevelList(false):Level::getLevelList();
        $type = $request->type? 1 : 0;
        return view('backend.flaw.levellist',[
            'lists'=>$lists,
            'type'=>$type,
            ]
        );
    }

    /**
     * addLevel 增加等级操作
     *
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
     */
    public function addLevel(Request $request){
        if($request->isMethod('post')){
            $data = $request->all();
            $validator = Level::validate($request->all());
            if($validator->fails()) {
                return redirect('admin/levelAdd')
                    ->withErrors($validator)
                    ->withInput();
            }
            Level::create($data);
            return redirect('/admin/levelList');
        }else{
            return view('backend.flaw.leveladd',['action'=>'add']);
        }
    }

    /**
     * updateLevel  更新等级操作
     *
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
     */
    public function updateLevel(Request $request){
        if($request->isMethod('post')){
            $validator = Level::validate($request->all());
            if($validator->fails()) {
                return redirect('admin/levelUpdate/id/'.$request->id)
                    ->withErrors($validator)
                    ->withInput();
            }
            $save = Level::find($request->id);
            $save->name = $request->name;
            $save->level = $request->level;
            $save->min_reward = $request->min_reward;
            $save->max_reward = $request->max_reward;
            $save->min_integ = $request->min_integ;
            $save->max_integ = $request->max_integ;
            $save->term = $request->term;
            $save->description = $request->description;
            $save->save();
            return redirect('/admin/levelList');
        }else{
            $item =  Level::find($request->id);
            return view('backend.flaw.leveladd',['item'=>$item,'action'=>'update']);
        }
    }

    /**
     * delLevel 删除等级操作-软删除
     *
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     * @throws \Exception
     */
    public function delLevel(Request $request){
        $count = Vuls::withTrashed()->where('level_id','=',$request->id)->count();
        if($count>0){
            abort(500,'该等级下面已有提交的漏洞，请先删除漏洞！');
        }
        Level::where('id', $request->id)->delete();
        return redirect('/admin/levelList');
    }

    /**
     * restoreLevel  从回收站中恢复删除的数据
     *
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function restoreLevel(Request $request){
        Level::withTrashed()->where('id', $request->id)->restore();
        return back();
    }

    /**
     * forceDelLevel 物理删除，即直接从数据库中删除数据
     *
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function forceDelLevel(Request $request){
        $childs = Level::withTrashed()->where('id', $request->id)->first()->getLevelVuls->isEmpty();
        if(!$childs){
            abort(500,'该等级下面已有提交的漏洞，请先删除漏洞！');
        }
        Level::withTrashed()->where('id', $request->id)->forceDelete();
        return back();
    }
}
